#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "nyu_string.h"

static NyuString *to_string(NyuObject *self);
static guint get_hash_code(NyuObject *self);
static gboolean equals(NyuObject *self, NyuObject *other);

NyuString *nyu_string_new(const char *str)
{
    NyuString *self;

    self = nyu_refcnt_new(NyuString);
    if (!self) {
        perror("cannot alloc NyuString!!");
        abort();
    }

    NYU_OBJECT_INIT(NYU_OBJECT(self), NYU_OBJECT_TYPE_STRING, nyu_object_destroy);

    NYU_OBJECT(self)->to_string = to_string;
    NYU_OBJECT(self)->get_hash_code = get_hash_code;
    NYU_OBJECT(self)->equals = equals;

    self->str = str;
    self->hash_code = g_str_hash(str);

    return self;
}

static NyuString *to_string(NyuObject *self)
{
    return NYU_STRING(self);
}

static guint get_hash_code(NyuObject *self)
{
    return NYU_STRING(self)->hash_code;
}

static gboolean equals(NyuObject *self, NyuObject *other)
{
    if (other->type != NYU_OBJECT_TYPE_STRING)
        return FALSE;
    return (strcmp(NYU_STRING(self)->str, NYU_STRING(other)->str) == 0);
}
